<?php 
require_once 'classes/mySession.php';
#IF NOT
if($pgSession->isUserLoggedIn()){
	$pgSession->redirect("./home.php");
}else{
	require_once 'classes/users.php';
#	CHECK POST PARAMETERS TO SEE IF IT IS A LOGIN REQUEST
	if(isset($_POST["loggingin"]) && trim($_POST["loggingin"])==='1'
		&& isset($_POST["email"]) && isset($_POST["password"])){
#		IF YES, CHECK PASSWORD AND IF OK PUT USER IN SESSION AND REDIRECT TO HOME
		$u = $userUtils->checkUserPassword($_POST["email"], $_POST["password"]);
		if($u["status"]){
			$pgSession->saveUserInSession($u["user"]);
			$pgSession->redirect("./login.php");
		}else{
			echo $u["message"];
		}
	}
#	CHECK IF PARAMETERS ARE VALID:
#		IF YES, SAVE IN DB, PUT USER IN SESSION AND REDIRECT TO HOME:
	else if(isset($_POST["email"]) 
		&& isset($_POST["name"])
		&& isset($_POST["password"]) && isset($_POST["password_confirm"])
		&& isset($_POST["signingin"]) && trim($_POST["signingin"])==='1'
	){
		$allOK = TRUE;
		if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)){
			echo "Given email is invalid.";
			$allOK = FALSE;
		}
		if(!(trim(utf8_encode($_POST["password"]))===trim(utf8_encode($_POST["password_confirm"])))){
			echo "Password and its confirmation don't match.";
			$allOK = FALSE;
		}
		if(empty($_POST["name"]) || !is_string($_POST["name"])){
			echo "Given name is invalid.";
			$allOK = FALSE;
		}
		if($allOK){
			$u = new User(NULL,$_POST["email"]);
			$u->setName($_POST["name"]);
			$u->setPassword(utf8_encode($_POST["password"]));
			
			$resp = $userUtils->saveUser($u);
			
			if($resp["status"]){
				$pgSession->saveUserInSession($resp["user"]);
				$pgSession->redirect("./login.php");
			}else{
				echo $resp["message"];
			}
		}
	}
}
#		IF NO:
#		BUILD LOGIN BOX
#		PUT SUBSCRIBE, ABOUT AND CONTACT LINK
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="pt-BR">
<head>
	<title>PG++ :: Ferramenta de Anotação de Localização em Fotografias</title>
	<meta charset="utf-8">
	<meta name="description" content="Ferramenta de anotação de localização em coleções de fotografias pessoais.">
	<meta name="author" content="Davi Serrano e George Henrique">

	<link rel="stylesheet" href="css/reset.css">
	<link rel="stylesheet" href="css/styles.css">
	<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700' rel='stylesheet' type='text/css'>
</head>

<body>

	<header>
		<div class="topo">
			<a href="#"><img class="logo" src="images/logo.png"/></a>
		</div>
	</header>
	
	<div class="clear"></div>
	
	<section>
		<div class="conteudo">
			<div class="inicial">
				<h1>Bem-vindo ao PG++</h1>
				<h2>Organize suas fotografias de maneira simples e fácil. <a href="http://dx.doi.org/10.1109/JCDL.2004.1336098">Estudos</a> comprovam que o lugar onde a fotografia foi capturada é uma das primeiras informações que as pessoas lembram quando querem recuperar aquela foto. Anote rapidamente a localização das suas fotografias e acompanhe</h2>
				
				<table align="center" style="margin:auto;"><tr><td align="center">
				
				<div class="formularios">
					<div class="login">
						<p class="titulo-formulario">Log In</p>
						<form class="formulario" method="post" action="./login.php">
							<input id="i1" type="text" name="email" placeHolder="E-mail" class="icon-email" autocomplete="off"/>
							<input id="i2"  type="password" name="password" placeHolder="Senha" class="icon-senha" autocomplete="off"/>
							<input type="hidden" name="loggingin" value="1"/>
							<input type="submit" value="Log In"/>
						</form>
					</div>
					<div class="cadastro">
						<p class="titulo-formulario">Cadastro</p>
						<form class="formulario" method="post" action="./login.php">
							<input id="i3" type="text" name="name" placeHolder="Nome" class="icon-nome" autocomplete="off"/>
							<input id="i4" type="text" name="email" placeHolder="E-mail" class="icon-email" autocomplete="off"/>
							<input id="i5" type="password" name="password" placeHolder="Senha" class="icon-senha" autocomplete="off"/>
							<input id="i6" type="password" name="password_confirm" placeHolder="Confirmar Senha" class="icon-senha" autocomplete="off"/>
							<input type="hidden" name="signingin" value="1"/>
							<input type="submit" value="Cadastro"/>
						</form>
					</div>
				</div>
				
				</td></tr></table>
				
			</div>
		</div>
	</section>
	
	<div class="clear"></div>
	
	<footer>
		<nav class="rodape">
			<a href="#">SOBRE</a>
			<a href="#">CONTATO</a>
		</nav>
		<br>
	</footer>
	
</body>
</html>
